/* 
 * MacHash - A simple utility that takes a filename or a string and creates
 * a digest of the contents using the select hash type.  MacHash acts as a 
 * Cocoa UI to the OpenSSL hash libraries.
 *
 * Copyright (C) 2006 Rick Russell - machash at idatasys dot com
 * 
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */
 

#import "MainController.h"


@implementation MainController


- (id)init
{
	[super init];
	
	NSLog(@"init");
	hashClass = [[Hash alloc] init];
	
	return self;
}

- (void)awakeFromNib
{
	// Clear out the labels
	[hashResult setStringValue:@""];
	[filePathLabel setStringValue:@""];
	
}

- (IBAction)doHash:(id)sender
{
	if([btnHash title] == @"Clear")
	{
		[self clearHashResult];

	}
	else
	{
		NSString *input = [textToHash string];
		NSString *digestType = [hashType titleOfSelectedItem]; 
		[hashClass createStringHash:input hashType:digestType];
		[hashResult setStringValue:[hashClass createStringHash:input hashType:digestType]];
		[btnCopyHash setHidden:NO];
	}
	
}

- (IBAction)hashTypeChange:(id)sender
{
	if(isFileDrop)
		[self fileDrop:[filePathLabel stringValue]];
	else
		[self doHash:self];
	
	// [self clearHashResult];
}

- (void)dealloc
{
	NSLog(@"dealloc");
	
	[hashClass release];
	[super dealloc];
}

-(void)fileDrop:(id)filepath
{
	NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
	// Setup the GUI for the file Drop
	isFileDrop = YES;
	[textToHash setString:@""];
	[hashResult setStringValue:@""];
	[filePathLabel setStringValue:filepath]; 
	
	// Disable the controls until the process is completed
	[textToHash setEditable:NO];
	[btnHash setEnabled:NO];
	[hashType setEnabled:NO];
	[fileDropStatus startAnimation:self];
	[hashResult setStringValue:[hashClass createFileHash:(NSString *)filepath hashType:[hashType titleOfSelectedItem]]];

	// Enable the controls again after processing
	[btnHash setTitle:@"Clear"];
	[textToHash setEditable:YES];
	[btnHash setEnabled:YES];
	[hashType setEnabled:YES];
	[btnCopyHash setHidden:NO];
	[fileDropStatus stopAnimation:self];
	
	[pool release];
	return;
	
}

/*
 * Clear the hash result.
 */
- (void)clearHashResult
{
	isFileDrop = NO;
	// Set the label back to the default for a string hash
	// [dropLabel setStringValue:@"Drop a file here"];
	// [dropLabel setToolTip:@""];
	// [dropLabel setTextColor:[NSColor blackColor]];
	[hashResult setStringValue:@""];
	[filePathLabel setStringValue:@""];	
	[btnHash setTitle:@"Hash"];	
	[btnCopyHash setHidden:YES];
}


- (void)textDidChange:(NSNotification *)aNotification
{
	[self clearHashResult];
}

- (void)windowWillClose:(NSNotification *)aNotification
{
	[[NSApplication sharedApplication] terminate:self];
}

- (void)copyHash:(id)sender
{
	NSLog(@"Copy the Hash");
}




- (IBAction)openFile:(id)sender
{
	int returnCode;
	NSString *path;
	
	NSOpenPanel *panel = [NSOpenPanel openPanel];
	
	returnCode =[panel runModalForDirectory:NSHomeDirectory()
                    file:nil types:nil];

	//Did the user choose "Open"?
	if (returnCode == NSOKButton) {
			path = [panel filename];
			[self fileDrop:path];
	}
					
}

// Drag to the doc icon
- (BOOL)application:(NSApplication *)sender openFile:(NSString *)path
{
    [self fileDrop:path];
	return YES;
}

	
@end
